<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
 
    class importexcel extends CI_Controller{
        public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('username')) {
            redirect('login');
            return;
        }
        $this->data['sidebarhome']="";
	    $this->data['sidebardaftar']="";
	    $this->data['sidebardaftarclient']="";
	    $this->data['sidebardaftarfinance']="";
    	$this->data['sidebarinputdata']="";
    	
    	$this->data['sidebarcustomer']="";
    	$this->data['sidebarcabang']="";
    	$this->data['sidebaruser']="";

        //$this->load->library(array('PHPExcel','PHPExcel/IOFactory'));
        $this->load->model("mkaryawan");
        $this->load->library('PHPExcel');
         
    }
    
    public function index(){

    }
    
    public function importdatakaryawan(){
        $this->data['main_view'] = 'vimportdatakaryawan';
        $config['upload_path'] = './assets/files/'; //realpath(APPPATH).'\\' ;
        $config['allowed_types'] = 'xls|xlsx';
        $config['max_size']      = 5000000;
        $config['remove_spaces'] = true;
        $this->load->library('upload',$config);
        
       if (isset($_POST['submit'])){ 
                if (! $this->upload->do_upload('import')) {
                    //variabel penanda keberhasilan upload
                   $this->session->set_flashdata('message', "File excel belum dipilih!");
                   //$this->session->set_flashdata('message', "File excel belum dipilih!".$this->upload->display_errors().$config['upload_path']);
                   redirect ('importexcel/importdatakaryawan');

                }
                else{ 
                    $ket_data = array('upload_data' => $this->upload->data());
                    foreach ($ket_data as $item => $value) {
                    if ($item == 'upload_data') {
                        $upload_data2 = $value;
                        $nama_file = $upload_data2['file_name'];
                        break;
                        }
                    }
                    $fileName = $_FILES['import']['name'];
                    $config['upload_path'] = './assets/files/';
                    $config['file_name'] = $fileName;
                    $config['allowed_types'] = 'xls|xlsx';
                    $config['max_size']        = 500000;
                
                    $this->load->library('upload', $config);
                    $this->upload->initialize($config);
               
                    if(! $this->upload->do_upload('import') )
                    $this->upload->display_errors();
                
                    $media = $this->upload->data('import');
                    $inputFileName = './assets/files/'.$media['file_name'];

                //  Read your Excel workbook
                    try {
                        $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
                        $objReader = PHPExcel_IOFactory::createReader($inputFileType);
                        $objPHPExcel = $objReader->load($inputFileName);
                    } catch(Exception $e) {
                        die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
                    }

                //  Get worksheet dimensions
                    $sheet = $objPHPExcel->getSheet(0);
                    $highestRow = $sheet->getHighestRow();
                    $highestColumn = $sheet->getHighestColumn();
                          
                //  Loop through each row of the worksheet in turn
                
                for ($row = 2; $row <= $highestRow; $row++){                  //  Read a row of data into an array                 
                                $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
                                                        NULL,TRUE,FALSE);
                 //  Insert row data array into your database of choice here
                     
                
                     $id_customer = $this->mkaryawan->mget_idCustomer($rowData[0][0]);
                     $id_cabang = $this->mkaryawan->mget_idCabang($rowData[0][6]);
                     $datarow = array(
						'username_creator' => $this->session->userdata('username'),
						'id_customer' => $id_customer ,
						'nik_customer' => $rowData[0][1],
						'nik_sjs' => $rowData[0][2],
						'nama_kar' => $rowData[0][3],
						'jabatan' => $rowData[0][4],
						'departement' => $rowData[0][5],
						'id_cabang' => $id_cabang,
						'status_kar' => $rowData[0][7],			
						'joint_awal' =>  date('Y-m-d', strtotime($rowData[0][8])),
						'total_cuti' => $rowData[0][10],
						'cuti_dipakai' => $rowData[0][11],
						'sisa_cuti' => $rowData[0][12],
						'bank' => $rowData[0][13],
						'no_rekening' => $rowData[0][14],
						'nama_rek' => $rowData[0][15],
						'jenis_kelamin' => $rowData[0][16],
						'tempat_lahir' => $rowData[0][17],
						'tanggal_lahir' => date('Y-m-d', strtotime($rowData[0][18])),
						'agama' => $rowData[0][19],
						'alamat_ktp' => $rowData[0][20],
						'rt' => $rowData[0][21],
						'rw' => $rowData[0][22],
						'kode_pos' => $rowData[0][23],
						'kecamatan' => $rowData[0][24],
						'desa' => $rowData[0][25],
						'kabupaten' => $rowData[0][26],
						'alamat_tinggal' => $rowData[0][27],
						'rt_tinggal' => $rowData[0][28],
						'rw_tinggal' => $rowData[0][29],
						'kode_pos_tinggal' => $rowData[0][30],
						'kecamatan_tinggal' => $rowData[0][31],
						'desa_tinggal' => $rowData[0][32],
						'kabupaten_tinggal' => $rowData[0][33],
						'tel_rumah' => $rowData[0][34],
						'tel_hp' => $rowData[0][35],
						'tel_hp2' => $rowData[0][36],
						'tel_keluarga' => $rowData[0][37],
						'hub_keluarga' => $rowData[0][38],
						'nama_keluarga' => $rowData[0][39],
						'pendidikan' => $rowData[0][40],
						'nama_sekolah' => $rowData[0][41],
						'jurusan' => $rowData[0][42],
						'jenis_identitas' => $rowData[0][43],
						'no_identitas' => $rowData[0][44],
						'no_npwp' => $rowData[0][45],
						'no_jamsostek' => $rowData[0][47],
						'bpjs' => $rowData[0][46],
						//'nama_asuransi' => $rowData[0][3],
						//'no_asuransi' => $rowData[0][3],
						//'kode_jamsostek' => $rowData[0][3],
						'ppk_jamsostek' => $rowData[0][48],
						//'tipe_jaminan' => $rowData[0][3],
						'no_jaminan' => $rowData[0][49],
						'no_bpkb' => $rowData[0][50],
						'penyimpanan_jaminan' => $rowData[0][51],
						'nama_ibu' => $rowData[0][52],
						'status_perkawinan' => $rowData[0][53],
						'no_kk' => $rowData[0][54],
						'nama_suami_istri' => $rowData[0][55],
						'no_ktp_nik_pasangan' => $rowData[0][56],
						'jenis_kelamin_si' => $rowData[0][57],
						'tempat_lahir_si' => $rowData[0][58],
						'tgl_lahir_si' => date('Y-m-d', strtotime($rowData[0][59])),
						'nama_anak1' => $rowData[0][60],
						'jenis_kelamin_anak1' => $rowData[0][61],
						'tempat_lahir_anak1' => $rowData[0][62],
						'tgl_lahir_anak1' => date('Y-m-d', strtotime($rowData[0][63])),
						'nama_anak2' => $rowData[0][64],
						'jenis_kelamin_anak2' => $rowData[0][65],
						'tempat_lahir_anak2' => $rowData[0][66],
						'tgl_lahir_anak2' => date('Y-m-d', strtotime($rowData[0][67])),
						'nama_anak3' => $rowData[0][68],
						'jenis_kelamin_anak3' => $rowData[0][69],
						'tempat_lahir_anak3' => $rowData[0][70],
						'tgl_lahir_anak3' => date('Y-m-d', strtotime($rowData[0][71])),
						'nama_anak4' => $rowData[0][72],
						'jenis_kelamin_anak4' => $rowData[0][73],
						'tempat_lahir_anak4' => $rowData[0][74],
						'tgl_lahir_anak4' => date('Y-m-d', strtotime($rowData[0][75])),
						'no_pkwt1' => $rowData[0][76],
						'awal_kontrak_pkwt1' => date('Y-m-d', strtotime($rowData[0][77])),
						'akhir_kontrak_pkwt1' => date('Y-m-d', strtotime($rowData[0][78])),
						'lama_kontrak1' => $rowData[0][79],
						'no_pkwt2' => $rowData[0][80],
						'awal_kontrak_pkwt2' => date('Y-m-d', strtotime($rowData[0][81])),
						'akhir_kontrak_pkwt2' => date('Y-m-d', strtotime($rowData[0][82])),
						'lama_kontrak2' => $rowData[0][83],
						'no_pkwt3' => $rowData[0][84],
						'awal_kontrak_pkwt3' => date('Y-m-d', strtotime($rowData[0][85])),
						'akhir_kontrak_pkwt3' => date('Y-m-d', strtotime($rowData[0][86])),
						'lama_kontrak3' => $rowData[0][87],
						//'id_card_status' => $rowData[0][3],
						'no_id_card' => $rowData[0][98],
						'tanggal_sp1' => date('Y-m-d', strtotime($rowData[0][88])),
						'alasan_sp1' => $rowData[0][89],
						'tanggal_sp2' => date('Y-m-d', strtotime($rowData[0][90])),
						'alasan_sp2' => $rowData[0][91],
						'tanggal_sp3' => date('Y-m-d', strtotime($rowData[0][92])),
						'alasan_sp3' => $rowData[0][93],
						'status_phk' => $rowData[0][94],
						'mutasi_dari' => $rowData[0][95],
						'mutasi_ke' => $rowData[0][96],
						'tanggal_mutasi' => date('Y-m-d', strtotime($rowData[0][97])),
						'tanggal_berhenti' => date('Y-m-d', strtotime($rowData[0][99])),
						'alasan_berhenti' => $rowData[0][100],
						'keterangan' => $rowData[0][101],
						'gaji_pokok' => $rowData[0][102],
						'allowance' => $rowData[0][103],
						'uang_kehadiran' => $rowData[0][104],
						'overtime' => $rowData[0][105],
						'tunjangan_jabatan' => $rowData[0][106],
						'tunjangan_transport' => $rowData[0][107],
						'tunjangan_makan' => $rowData[0][108],
						'tunjangan_pulsa' => $rowData[0][109],
						'tunjangan_shift' => $rowData[0][110],
						'komisi' => $rowData[0][111],
						'incentive' => $rowData[0][112],
						'bonus' => $rowData[0][113],
						'adjustment' => $rowData[0][114],
						'lain' => $rowData[0][115],                

                              );    
                     
                    $this->mkaryawan->insert($datarow);  
                     
                  }
                              
//             $data['hasil'] = 'berhasil';
               $this->session->set_flashdata('messages', 'Proses import data telah berhasil dilakukan!, '. ($highestRow-1) .' data telah berhasil ditambahkan');
               redirect('importexcel/importdatakaryawan');
            } 
           
         }
       $this->load->view('template', $this->data);    
           
    }
}